﻿using System.Globalization;
using System.Threading;
using System.Windows.Input;
using System.Windows.Media;
using GalaSoft.MvvmLight.Command;
using ZOIL.DataModel;

namespace ZOIL.Library.Components.Notepad
{
    [ViewModelFor(typeof (NotepadView))]
    public class NotepadViewModel : BaseViewModel
    {
        #region commands

        public RelayCommand CloseNotepadCommand { get; private set; }

        #endregion

        private DrawingImage _pathImage;

        public NotepadViewModel()
        {
            CloseNotepadCommand = new RelayCommand(OnCloseNotepad);
        }

        private void OnCloseNotepad()
        {
            var rc = DatabaseCollection.Collection;
            var pp = rc.GetPersistableAndVisualProperties(((IPersistable) Model).DatabaseId);
            rc.Remove(pp);
        }

        public Geometry Strokes
        {
            get
            {
                var oldCultureInfo = Thread.CurrentThread.CurrentCulture;
                var oldUICultureInfo = Thread.CurrentThread.CurrentUICulture;
                Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
                Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;

                var sg = Geometry.Parse((Model as Notepad).Strokes);                

                Thread.CurrentThread.CurrentCulture = oldCultureInfo;
                Thread.CurrentThread.CurrentUICulture = oldUICultureInfo;

                return sg;
            }
        }

        public DrawingImage PathImage
        {
            get
            {
                if (_pathImage == null)
                {
                    var gd = new GeometryDrawing();
                    gd.Geometry = Strokes;
                    gd.Brush = Brushes.Black;
                    _pathImage = new DrawingImage(gd);
                }

                return _pathImage;
            }
        }
    }
}
